package com.wing.system.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.system.model.entity.Dic;
import com.wing.system.model.response.DicVO;
import com.wing.system.service.DicService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.system.model.request.DicForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-字典数据管理", tags = "字典数据管理")
@RestController("appDicController")
@RequestMapping("system/app/dic")
@Slf4j
public class DicController {

    @Autowired
    private DicService dicService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping("/list")
    public PageResult<DicVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String dicTypeCode
    ) {
        log.info("dicTypeCode:{}",dicTypeCode);
        Page<DicVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("dicTypeCode",dicTypeCode);
        Page<DicVO> list = dicService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long dicId) {

        DicVO dicVO = dicService.get4appById(dicId);
        if (dicVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), dicVO);
    }

    @PostMapping()
    public JsonResult add(@Validated DicForm dicForm) {

        if (dicService.add(dicForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
